package com.wechat.backend.service.impl;

import com.wechat.backend.entity.Session;
import com.wechat.backend.entity.User;
import com.wechat.backend.repository.SessionRepository;
import com.wechat.backend.repository.UserRepository;
import com.wechat.backend.service.SessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SessionServiceImpl implements SessionService {

    @Autowired
    private SessionRepository sessionRepository;

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<Map<String, Object>> getSessionList(Integer userId) {
        List<Session> sessions = sessionRepository.findByUserId(userId);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Session s : sessions) {
            User user = userRepository.findById(s.getFriendId());
            if (user == null) continue;
            Map<String, Object> map = new HashMap<>();
            map.put("id", user.getId());
            map.put("nickname", user.getNickname());
            map.put("avatar", user.getAvatar());
            map.put("lastMessage", s.getLastMessage());
            map.put("unreadCount", s.getUnreadCount());
            result.add(map);
        }
        return result;
    }
} 